package net.tngou.iask.action.api;

import java.util.HashMap;
import java.util.List;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.enums.LevelEnum;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Question;
import net.tngou.iask.pojo.Question_answer;
import net.tngou.iask.util.StringUtil;
/**
 * 问题选项接口
 * @author tngou@tngou.net
 *
 */
public class AnswerApi extends BaseAction {
	/**
	 * 添加问题选项
	 */
	@SuppressWarnings("serial")
	public void add() {
		Answer bean = getAsk(Answer.class);
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有添加权限");return;
		}
		if(isEmpty(bean.getName()))
		{
			run_false("请求的参数不正确！");return;
		}
		Answer pojo = bean.get(new HashMap<String, Object>(){{
			put("name", bean.getName());
			put("type", bean.getType());
			put("main", 0);
		}});
		if(pojo!=null)
		{
			run_false("已经存在该选项！");return;
		}
		if(user.getMain()!=0)
		{
			pojo = bean.get(new HashMap<String, Object>(){{
				put("name", bean.getName());
				put("type", bean.getType());
				put("main", user.getMain());
			}});
			if(pojo!=null)
			{
				run_false("已经存在该选项！");return;
			}
		}
		
		bean.setMain(user.getMain());
		long b = bean.save();
		if(b>0){
			saveLog(LevelEnum.system, "添加问题选项(id:"+b+"-"+bean.getName()+")");
			run_success(b+"");return;
		}
		else {run_false("添加失败！");return;}
	}
	
	/**
	 * 更新
	 */
	public void update() {
		Answer bean = getAsk(Answer.class);
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有添加权限");return;
		}
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Answer answer = new Answer().get(bean.getId());
		if(answer==null||answer.getMain()!=user.getMain())
		{
			run_false("没有编辑的数据");return;
		}
		if(StringUtil.isNotEmpty(bean.getName())&&!bean.getName().equals(answer.getName()))
		{
			@SuppressWarnings("serial")
			POJO pojo = bean.get(new HashMap<String, Object>(){{
				put("name", bean.getName());
				put("type", answer.getType());
			}});
			if(pojo!=null)
			{
				run_false("已经存在该选项！");return;
			}
			answer.setName(bean.getName());
		}
		
		if(request.getParameter("value")!=null)answer.setValue(bean.getValue());
		if(request.getParameter("type")!=null)answer.setType(bean.getType());
		long b = answer.update();
		if(b>0){
			saveLog(LevelEnum.system, "编辑问题选项(id:"+answer.getId()+"-"+answer.getName()+")");
			run_success("编辑成功！");return;
		}else{
			run_false("编辑失败");return;
		}
		
	}
	/**
	 * 删除
	 */
	public void delete() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有删除权限");return;
		}
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Answer answer = new Answer().get(ask.getId());
		if(answer==null)
		{
			run_false("没有删除的数据");return;
		}
		if(answer.getMain()!=user.getMain())
		{
			run_false("没有删除权限");return;
		}
		List<? extends POJO> list = new Question_answer().list(new HashMap<String, Object>(){{
			put("answer", answer.getId());
		}});
		if(list!=null&&!list.isEmpty())
		{
			run_false("该选项已经绑定，不能删除");return;
		}
		boolean b = answer.delete();
		if(b){
			saveLog(LevelEnum.system, "删除问题选项(id:"+answer.getId()+"-"+answer.getName()+")");
			run_success("删除成功！");return;
		}else{
			run_false("删除失败");return;
		}
	}
	
	public void list() {
		
	}
	
}
